// TypeScript file
class XMLHttp {
    constructor() {

    }
    static obj = null;
    static Create() {
        if (!this.obj) {
            this.obj = new this;
        }
        return this.obj;
    }
    public sendGetRequest(_postObj): void {
        _postObj = _postObj || {};
        var request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.open(_postObj.URL + _postObj.Params, egret.HttpMethod.GET);
        request.send();
        request.addEventListener(egret.Event.COMPLETE, _postObj.Callback, this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onGetIOError, this);
        request.addEventListener(egret.ProgressEvent.PROGRESS, this.onGetProgress, this);
    }
    private onGetComplete(event: egret.Event): void {
        var request = <egret.HttpRequest>event.currentTarget;
        console.log("get data : ", request.response);
    }
    private onGetIOError(event: egret.IOErrorEvent): void {
        console.log("get error : " + event);
    }
    private onGetProgress(event: egret.ProgressEvent): void {
        console.log("get progress : " + Math.floor(100 * event.bytesLoaded / event.bytesTotal) + "%");
    }
    /*
    *
    *   _url:string = "", (请求的链接)
    *   _requestData = null,(要发送的数据)
    *   _tokenkey:string ="",(秘钥)
    *   MD5: true (是否MD5加密)
    *   callback (回调函数)
    *   {_url:"",_tokenkey:"lkasdj213489ulkjadsf",MD5:true,function abc(params:type) {
        
    }}
    */
    public sendPostRequest(_postObj) {
       
        // let tokenkey = "lkasdj213489ulkjadsf";
        // let requestData ={"phone":"15817142998"};
        // let url = "http://swin.bluej.cn/index.php/Home/app/verification_code.html";
        _postObj = _postObj || {};
        let requestData = JSON.stringify(_postObj.RequestData);
        let params = "";
        if (_postObj.MD5 == true) {
            let key = hex_md5(requestData + _postObj.Tokenkey);
            params = "data=" + requestData + "&" + "key=" + key;
        } else {
            params = "data=" + requestData;
        }

        let request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;

        request.open(_postObj.URL, egret.HttpMethod.POST);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        request.send(params);
        request.addEventListener(egret.Event.COMPLETE, this.onPostComplete, this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onPostIOError, this);
        request.addEventListener(egret.ProgressEvent.PROGRESS, this.onPostProgress, this);
    }

    private onPostComplete(event: egret.Event) {
        let request = <egret.HttpRequest>event.currentTarget;
        // console.log("post data : ",request.response);
        // console.log(request.response.substring(0, 50));
        // console.log("POST response:\n" + request.response.substring(0, 50) + "...");
        // this.statusPostLabel.text = "Get POST response!";
    }
    private onPostIOError(event: egret.IOErrorEvent): void {
        console.log("post error : " + event);
    }
    private onPostProgress(event: egret.ProgressEvent): void {
        console.log("post progress : " + Math.floor(100 * event.bytesLoaded / event.bytesTotal) + "%");
    }
}